package com.hylanda.config;

import java.sql.SQLException;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import com.alibaba.druid.pool.DruidDataSource;

/** 
 * @author zhangy
 * @E-mail:blackoon88@gmail.com 
 * @qq:846579287
 * @version created at：2018年1月18日 下午3:41:15 
 * note
 */

@Configuration
public class SqlServerConfig {

		private int initialSize=10;
		private String dbType="sqlserver";
		private int minIdle=5;
		private int maxActive=20;
		private int maxWait=60000;
		private int timeBetweenEvictionRunsMillis=60000;
		private int minEvictableIdleTimeMillis=300000;
		private String validationQuery="SELECT 'x'";
		private boolean testWhileIdle=true;
		private boolean testOnBorrow=false;
		private boolean testOnReturn=false;
		private boolean poolPreparedStatements=true;
		private int maxPoolPreparedStatementPerConnectionSize=20;
		private String filters="stat";
		private String driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver";
		private String jdbcUrl="jdbc:sqlserver://l6xxc5w2m8.database.chinacloudapi.cn:1433;DatabaseName=platform";
		private String username="adm";
		private String password="Hailiang888";
		
		@Bean("sqlserverDataSource")
		public DruidDataSource sqlserverDatasource(){
			DruidDataSource druidDataSource=new DruidDataSource();
			druidDataSource.setInitialSize(initialSize);
			druidDataSource.setDbType(dbType);
			druidDataSource.setMinIdle(minIdle);
			druidDataSource.setMaxActive(maxActive);
			druidDataSource.setMaxWait(maxWait);
			druidDataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
			druidDataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
			druidDataSource.setValidationQuery(validationQuery);
			druidDataSource.setTestWhileIdle(testWhileIdle);
			druidDataSource.setTestOnBorrow(testOnBorrow);
			druidDataSource.setTestOnReturn(testOnReturn);
			druidDataSource.setPoolPreparedStatements(poolPreparedStatements);
			druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
			try {
				druidDataSource.setFilters(filters);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			druidDataSource.setDriverClassName(driverClass);
			druidDataSource.setUrl(jdbcUrl);
			druidDataSource.setUsername(username);
			druidDataSource.setPassword(password);
			return druidDataSource;
		}
		@Bean("sqlseverJdbcTemplate")
		public JdbcTemplate sqlseverJdbcTemplate(){
			JdbcTemplate JdbcTemplate=new JdbcTemplate(sqlserverDatasource());
			return JdbcTemplate;
		}
}

